package cn.iocoder.yudao.module.water.controller.admin.prosludge;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.water.controller.admin.prosludge.vo.*;
import cn.iocoder.yudao.module.water.dal.dataobject.prosludge.ProSludgeDO;
import cn.iocoder.yudao.module.water.convert.prosludge.ProSludgeConvert;
import cn.iocoder.yudao.module.water.service.prosludge.ProSludgeService;

@Api(tags = "管理后台 - 污泥生产")
@RestController
@RequestMapping("/water/pro-sludge")
@Validated
public class ProSludgeController {

    @Resource
    private ProSludgeService proSludgeService;

    @PostMapping("/create")
    @ApiOperation("创建污泥生产")
    @PreAuthorize("@ss.hasPermission('water:pro-sludge:create')")
    public CommonResult<Long> createProSludge(@Valid @RequestBody ProSludgeCreateReqVO createReqVO) {
        return success(proSludgeService.createProSludge(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新污泥生产")
    @PreAuthorize("@ss.hasPermission('water:pro-sludge:update')")
    public CommonResult<Boolean> updateProSludge(@Valid @RequestBody ProSludgeUpdateReqVO updateReqVO) {
        proSludgeService.updateProSludge(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除污泥生产")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('water:pro-sludge:delete')")
    public CommonResult<Boolean> deleteProSludge(@RequestParam("id") Long id) {
        proSludgeService.deleteProSludge(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得污泥生产")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('water:pro-sludge:query')")
    public CommonResult<ProSludgeRespVO> getProSludge(@RequestParam("id") Long id) {
        ProSludgeDO proSludge = proSludgeService.getProSludge(id);
        return success(ProSludgeConvert.INSTANCE.convert(proSludge));
    }

    @GetMapping("/list")
    @ApiOperation("获得污泥生产列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('water:pro-sludge:query')")
    public CommonResult<List<ProSludgeRespVO>> getProSludgeList(@RequestParam("ids") Collection<Long> ids) {
        List<ProSludgeDO> list = proSludgeService.getProSludgeList(ids);
        return success(ProSludgeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得污泥生产分页")
    @PreAuthorize("@ss.hasPermission('water:pro-sludge:query')")
    public CommonResult<PageResult<ProSludgeRespVO>> getProSludgePage(@Valid ProSludgePageReqVO pageVO) {
        PageResult<ProSludgeDO> pageResult = proSludgeService.getProSludgePage(pageVO);
        return success(ProSludgeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出污泥生产 Excel")
    @PreAuthorize("@ss.hasPermission('water:pro-sludge:export')")
    @OperateLog(type = EXPORT)
    public void exportProSludgeExcel(@Valid ProSludgeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ProSludgeDO> list = proSludgeService.getProSludgeList(exportReqVO);
        // 导出 Excel
        List<ProSludgeExcelVO> datas = ProSludgeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "污泥生产.xls", "数据", ProSludgeExcelVO.class, datas);
    }

}
